



import 'dart:convert';
import 'dart:io';

import 'package:aliplayer_widget/aliplayer_widget_lib.dart';
import 'package:flutter/material.dart';
import 'package:flutter_aliplayer/flutter_aliplayer.dart';
import 'package:flutter_aliplayer/flutter_aliplayer_factory.dart';
// import 'package:http/http.dart' as http show Client, get; // 此处
import 'package:http/io_client.dart';
import 'package:web_socket/web_socket.dart';

class MyVideo extends StatefulWidget {
  const MyVideo({super.key});

  @override
  State<MyVideo> createState() => _MyVideoState();
}

class _MyVideoState extends State<MyVideo>{

  late FlutterAliplayer fAliplayer;
  late ModeType _playMode;
  late AliPlayerWidgetController _controller;
  @override
  void initState() {
    //   initState
    super.initState();
     ()  async{
      // final socket = await OkHttpWebSocket.connect(
      //     Uri.parse('wss://ws.postman-echo.com/raw'));
      // socket.events.listen((e) async {
      //   switch (e) {
      //     case TextDataReceived(text: final text):
      //       print('Received Text: $text');
      //       await socket.close();
      //     case BinaryDataReceived(data: final data):
      //       print('Received Binary: $data');
      //     case CloseReceived(code: final code, reason: final reason):
      //       print('Connection to server closed: $code [$reason]');
      //   }
      // });


      // final http.Client httpClient;
      // if (Platform.isAndroid) {
      //   httpClient = OkHttpClient();
      // } else {
      //   httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
      // }
      //
      // final response = await httpClient.get(
      //   Uri.http(
      //     '192.168.0.237:8080',
      //     '/hello',
      //     {'q': "q", 'maxResults': '20', 'printType': 'books'},
      //   ),
      // );
      //
      // // final json = jsonDecode(utf8.decode(response.bodyBytes)) as Map;
      // // return Book.listFromJson(json);
      // httpClient.close();
    }();



    // FlutterAliplayer _aliPlayer = FlutterAliplayer();
    //
    // // 设置播放凭证
    // String playAuth = "your-play-auth-string"; // 替换为实际的播放凭证
    // _aliPlayer.setAuthInfo(playAuth);



    // http.get(Uri.parse("http://192.168.0.237:8080/hello"));
    _controller = AliPlayerWidgetController(context);
    // 配置播放器组件数据


    VideoSource  data = VideoSourceFactory.createVidAuthSource(
      vid: "a014d47a3abb71f0bb4b4531858c0102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Njg1Mjk0NWI1NDhiYzEtMDAwMDUuanBnIiwiRHVyYXRpb24iOjExMzIuOTA2Nn0sIkFjY2Vzc0tleUlkIjoiU1RTLk5YTnRZWnlFSkFZYWZiYzY4OXdZOHQ3YksiLCJQbGF5RG9tYWluIjoidmlkZW8uY3N3eC5jb20iLCJBY2Nlc3NLZXlTZWNyZXQiOiJDN21oZzNpTFRwbVQ3YTZvd3h1VFdXWXhFUkt2aVdLdVNqZldhUXBXb2cxYiIsIlJlZ2lvbiI6ImNuLXNoYW5naGFpIiwiQ3VzdG9tZXJJZCI6MTgyNDY4MzUxNzI1NTQ1Mn0=',
      // videoUrl: "http://192.168.0.103:5500/屏幕录制 2025-02-25 092100.mp4", // 替换为实际视频地址
      // coverUrl: "https://example.com/cover.jpg", // 替换为实际封面图地址
      videoSource: data,
      videoTitle: "Sample Video",
    );

    _controller.configure(data1);

    // _controller
  }
  @override
  void dispose() {
    //   dispose
    super.dispose();
    _controller.destroy();
  }
  @override
  Widget build(BuildContext context) {
    // 
    return AliPlayerWidget(
      _controller,
    );
  }
}

enum ModeType {
  URL,
  STS,
  AUTH
}

